package games.eightPuzzle;

import games.eightPuzzle.states.SlidingPuzzleState;

import java.util.ArrayList;
import java.util.Random;

import commonDataStructures.BinaryHeap;
import commonDataStructures.PrimitiveCollection;

public class GeneticBinaryHeap implements PrimitiveCollection<SlidingPuzzleState>{
	private BinaryHeap<SlidingPuzzleState> _benaryHeap;
	
	public GeneticBinaryHeap(){
		_benaryHeap = new BinaryHeap<SlidingPuzzleState>();
	}
	@Override
	public void init() {
		 _benaryHeap.init();
		 
		
	}

	@Override
	public void insert(SlidingPuzzleState e) {
		_benaryHeap.insert(e);
		
	}

	@Override
	public boolean isEmpty() {
		return _benaryHeap.isEmpty();
	}

	@Override
	public SlidingPuzzleState remove() {
		ArrayList<SlidingPuzzleState> tbank = new ArrayList<SlidingPuzzleState>();
		tbank.add(_benaryHeap.remove());
		while (!_benaryHeap.isEmpty()){
			SlidingPuzzleState s = _benaryHeap.remove();
			if (s.compareTo(tbank.get(0))!=0){
				_benaryHeap.insert(s);
				break;
			}else{
				tbank.add(s);
			}
		}
		Random rnd = new Random();
		SlidingPuzzleState ret = tbank.remove(rnd.nextInt(tbank.size()));
		//for(SlidingPuzzleState s:tbank){
		//	_bank.insert(s);
		//}
		return ret;
	}

	@Override
	public int size() {
		return _benaryHeap.size();
	}
	
	

}
